package cn.t.converter.user;

import cn.t.model.user.UserQueryDO;
import cn.t.persistence.entity.user.UserQueryPO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author l
 */
@Component
public class UserQueryUnquotedConverter {

    /**
     * 数据库对象转领域对象
     */
    public UserQueryDO po2ModelUnQuoted(UserQueryPO source) {
        UserQueryDO userQueryDO = userQueryConverter.po2Model(source);

        userQueryDO.setCardCode(trimFirmPrefix(trimQuote(source.getCardCode())));
        userQueryDO.setUserName(trimQuote(source.getUserName()));
        userQueryDO.setEmployeeCode(trimFirmPrefix(trimQuote(source.getEmployeeCode())));
        userQueryDO.setPhoneNumber(trimQuote(source.getPhoneNumber()));
        userQueryDO.setIndividualName(trimQuote(source.getIndividualName()));

        return userQueryDO;
    }

    public String trimFirmPrefix(String fieldValue) {
        if (StringUtils.isBlank(fieldValue) || !fieldValue.contains("_")) {
            return fieldValue;
        }
        return fieldValue.split("_")[1];
    }


    public String trimQuote(String fieldValue) {
        // 在这里实现你的自定义逻辑

        if (StringUtils.isBlank(fieldValue)) {
            return "";
        }

        fieldValue = fieldValue.replaceAll("\"", "");
        fieldValue = fieldValue.replaceAll("null", "");


        return fieldValue;
    }

    @Autowired
    private UserQueryConverter userQueryConverter;
}

